package com.example.jeson.stocktest;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;

import com.example.jeson.stocktest.mvp.IMainView;
import com.example.jeson.stocktest.mvp.MainPresenter;
import com.example.jeson.stocktest.mvp.adapter.MainAdapter;
import com.yqritc.recyclerviewflexibledivider.HorizontalDividerItemDecoration;

public class MainActivity extends AppCompatActivity implements IMainView {

    RecyclerView myRv;
    MainPresenter myPresenter;
    MainAdapter myAdapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        myRv = (RecyclerView) findViewById(R.id.rv);

        LinearLayoutManager layoutManager = new LinearLayoutManager(this);
        layoutManager.setSmoothScrollbarEnabled(true);
        myRv.setLayoutManager(layoutManager);
        myRv.setHasFixedSize(true);
        myRv.addItemDecoration(new HorizontalDividerItemDecoration.Builder(this)
                .drawable(R.color.whiteBackground)
                .size(32)
                .build());

        myPresenter = new MainPresenter(this);
        myPresenter.getListFromAssets(this);

    }


    @Override
    public void initView() {
        runOnUiThread(new Runnable() {
            @Override
            public void run() {
                myAdapter = new MainAdapter(myPresenter.getMyMessageList(), myPresenter.getmStockMap());
                myRv.setAdapter(myAdapter);
            }
        });
    }

    @Override
    public void updateStocks() {
        runOnUiThread(new Runnable() {
            @Override
            public void run() {
                if (myAdapter != null) {
                    myAdapter.notifyDataSetChanged();
                    //myAdapter.notifyStock();
                }

            }
        });
    }

    @Override
    protected void onDestroy() {
        if (myPresenter != null) {
            myPresenter.stopThread();
        }
        super.onDestroy();
    }
}
